/***
 * author:jyh
 * date:2012-05-01
 * list页面基类
 * 封装了实体的新增、删除、修改、查看操作，
 * gridId:list页面中的gridId
 * quicksearchId：快速查询标签ID
 * advSearchId:高级查询标签ID
 * idProperty:实体的ID字段名称
 * dataSetHandler：dataSetHandler
 * queryHandler:queryHandler
 * deleteMethod:dataset中删除单个对象的方法名
 * deleteAllMethod:dataset中批量删除对象的方法名
 * quickSearchMethod:queryHandler中快速查询的方法名称,默认为quickSearch
 * advSearchMethod：queryHandler中高级查询的方法名,默认为advSearch
 * module:所属模块的中文名称,用来构建title
 * moduleName:所属模块的英文名称,用来构造新增、修改、查看的jsp文件名,如：addRole.jsp,editRole.jsp,viewRole.jsp中的Role
 * modulePath：//相对与WebRoot下的模块所在路径,如：/pages/sysmgr/role/
 * 
 */

Ext.define('Pearlsen.BaseList',{
	gridId:null,
	quicksearchId:null,
	advSearchId:null,
	idProperty:null,//model中的ID字段
	nameProperty:null,//model中的代表实体名称的字段
	dataSetHandler:null,
	queryHandler:null,
	deleteMethod:'delete',
	deleteAllMethod:'deleteAll',
	quickSearchMethod:'quickSearch',
	advSearchMethod:'advSearch',
	module:null,//所属模块的中文名称
	moduleName:null,//所属模块的英文名称,用来构造新增、修改、查看的jsp文件名,如：addRole.jsp,editRole.jsp,viewRole.jsp中的Role
	modulePath:null,//相对与WebRoot下的模块所在路径,如：/pages/sysmgr/role/
	_grid:null,
	constructor : function(config){
		if( Ext.isEmpty(config.gridId) ){
			throw new Error("没有设置gridId");
		}
		if( Ext.isEmpty(config.module) ){
			throw new Error("没有设置模块名");
		}		
		if( Ext.isEmpty(config.modulePath) ){
			throw new Error("没有设置模块路径");
		}
		if( Ext.isEmpty(config.moduleName) ){
			throw new Error("没有设置模块名");
		}
		if( Ext.isEmpty(config.idProperty) ){
			throw new Error("没有指定对象ID字段");
		}
		if( Ext.isEmpty(config.nameProperty) ){
			throw new Error("没有指定对象名称字段");
		}
		if( Ext.isEmpty(config.dataSetHandler) ){
			throw new Error("没有指定dataSetHandler");
		}
		if( Ext.isEmpty(config.queryHandler) ){
			throw new Error("没有指定queryHandler");
		}		
		
		
		this.deleteMethod = config.deleteMethod ?config.deleteMethod:'delete';
		this.deleteAllMethod = config.deleteAllMethod ?config.deleteAllMethod:'deleteAll';
		this.quickSearchMethod = config.quickSearchMethod ?config.quickSearchMethod:'quickSearch';
		this.advSearchMethod = config.advSearchMethod ?config.advSearchMethod:'advSearch';
		Ext.apply(this,config,true);
		GlobalEvent.addListener(config.moduleName+"_refresh_list",this.refreshGrid,this);	    					
	},
	/***
	 * 获取新增时的URL
	 * @returns {String}
	 */
	getAddUrl:function(){
		var url = this.modulePath +"add"+this.moduleName+".jsp";
		return url;
	},
	
	/***
	 * 获取修改时的URL
	 * @returns {String}
	 */
	getEditUrl:function(){
		var record = this.getSelectedOne();
		var objId = record.data[this.idProperty];
		var objName = record.data[this.nameProperty];
		var url = this.modulePath +"edit"+this.moduleName+".jsp?objId={0}&objName={1}";
		url = url.format(objId,objName);
		return url;
	},
	/***
	 * 获取查看时的URL
	 * @returns {String}
	 */
	getViewUrl:function(){
		var record = this.getSelectedOne();
		var objId = record.data[this.idProperty];
		var objName = record.data[this.nameProperty];
		var url = this.modulePath +"view"+this.moduleName+".jsp?objId={0}&objName={1}";
		url = url.format(objId,objName);
		return url;
	},
	
	/***
	 * 获取listpage中的grid
	 * @returns
	 */
	getGrid:function(){
		if( !this._grid ){
			if( this.gridId ){
				this._grid = Ext.getCmp(this.gridId);
			}else{
				this._grid = null;
				throw new Error("未指定gridId");
			}
		}
		return this._grid;
	},
	/***
	 * 得到grid选中的第一条记录
	 */
	getSelectedOne:function(){
		var records = this.getSelected();
		if( records && records.length > 0 ){
			return records[0];
		}
		return null;
	},
	/***
	 * 得到grid中所有选中的记录，返回[record]数组
	 */
	getSelected:function(){
		return this.getGrid().getSelectionModel().getSelection();
	},
	/***
	 * 双击grid事件
	 * @param view
	 * @param record
	 * @param item
	 * @param index
	 * @param e
	 * @param eOpts
	 */
	itemdblclick:function(view,record,item,index,e,eOpts){
		
	},
	/***
	 * 打开新增页面
	 */
	add:function(){
		var tabId = this.createTabId("add");
		parent.MainTab.addTab(tabId,"新增"+this.module,this.getAddUrl());
	},
	/***
	 * 打开修改页面
	 */
	edit:function(){
		var record = this.getSelectedOne();
		if( record ) {
			var tabId = this.createTabId("edit",record.data[this.idProperty]);
			parent.MainTab.addTab(tabId,"编辑"+this.module,this.getEditUrl());
		}else{
			Pearlsen.Utils.showInfoMsg('请先选中一条记录.');
		}
	},
	/***
	 * 打开查看页面
	 */
	view:function(){
		var record = this.getSelectedOne();
		if( record ) {
			var tabId = this.createTabId("view",record.data[this.idProperty]);
			parent.MainTab.addTab(tabId,"查看"+this.module,this.getViewUrl());
		}else{
			Pearlsen.Utils.showInfoMsg('请先选中一条记录.');
		}
	},
	/***
	 * 删除选中的数据
	 */
	del:function(){
		var records = this.getSelected();
     	if( Ext.isEmpty(records)){
     		Pearlsen.Utils.showInfoMsg('请先选中一条记录.');
     		return ;
     	}
     	if(records.length ==1 ){
     		this._delOne();
     	}else{
     		this._delAll();
     	}
	},
	_delOne:function(){
     	var me = this;
		var record = this.getSelectedOne();
     	if( Ext.isEmpty(record) ){
     		return ;
     	}
        Ext.MessageBox.confirm('删除提示', '确定要删除【'+record.data[this.nameProperty]+'】吗?',function(btn){
        	if(btn=='yes'){
        		if( !me.dataSetHandler ){
        			throw new Error("未设置没有指定dataSetHandler");
        		}
         	 	var ds=new DataSet(me.dataSetHandler,me.moduleName+"_dataset");
         	 	ds.setHandlerMethod(me.deleteMethod);
     			ds.addCondition("objId",record.data[me.idProperty]);
     			ds.execute(function(result){
     				if( result.success ){
     					me.refreshGrid();
     				}else{
     					Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
     				}
     			});
         	 }
		});
	},
	/***
	 * 删除所有选中的数据
	 */
	_delAll:function(){
     	var me = this;
		var records = this.getSelected();
     	if( Ext.isEmpty(records) ){
     		return ;
     	}
        Ext.MessageBox.confirm('删除提示', '确定要删除所有选中的记录吗?',function(btn){
        	if(btn=='yes'){
        		if( !me.dataSetHandler ){
        			throw new Error("未设置没有指定dataSetHandler");
        		}
				var ids = "";
				for (var i = 0; i < records.length; i++) {
					if (i != 0)
						ids += ",";
					ids += records[i].data[me.idProperty];
				}
         	 	var ds=new DataSet(me.dataSetHandler,me.moduleName+"_dataset");
         	 	ds.setHandlerMethod(me.deleteAllMethod);
     			ds.addCondition("objIds",ids);
     			ds.execute(function(result){
     				if( result.success ){
     					me.refreshGrid();
     				}else{
     					Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
     				}
     			});
         	 }
		});		
	},
	/***
	 * 刷新grid
	 */
	refreshGrid:function(){
		this.getGrid().getStore().load();
	},
	/***
	 * 打开高级查询
	 */
	showAdvSearch:function(){
		var advWin = Ext.getCmp(this.advSearchId);
		if(advWin){
			advWin.show();
		}
	},
	/***
	 * 执行快速查询
	 */
	quickSearch:function(value){
		var pStore=this.getGrid().getStore();
		pStore.setQuery(this.queryHandler,this.quickSearchMethod);
		pStore.addQueryField("searchValue", value);
		pStore.load();
		/*
		 var query = new Query(this.queryHandler,"_load_dataset");
		 query.setHandlerMethod(this.quickSearchMethod);		 
		 query.setURL('/query.asp');
		 query.addCondition("searchValue", value);
		 query.setCompletedFun(this.afterSearch);
		 query.setCompletedFunScope(this);
		 query.load();	
		 */	
	},
	afterSearch:function(success,dataset,response, opts){
		if(success){
			if( dataset ){
				var store=this.getGrid().getStore();
				store.loadRawData(dataset);
			}
		}else{
			
		}
	},	
	
	
	/***
	 * 执行高级查询
	 * @param conditions
	 */
	advSearch:function(conditions){
		var pStore=this.getGrid().getStore();
		pStore.setQuery(this.queryHandler,this.advSearchMethod);
		if(conditions && conditions.length > 0 ){
			for( var i=0;i < conditions.length ;i ++ ){
				pStore.addCondition(conditions[i]);
			}
		}
		pStore.load();
		
		/*
		 var query = new Query(this.queryHandler,"_load_dataset");
		 query.setHandlerMethod(this.advSearchMethod);		 
		 query.setURL('/query.asp');
		 query.setConditions(conditions);
		 query.setCompletedFun(this.afterSearch);
		 query.setCompletedFunScope(this);
		 query.load();	
		 */
	},
	createTabId : function(ops, objId) {
		var tabId = this.getTabId() + '_' + ops;
		if (objId)
			tabId += objId;
		return tabId;
	},
	getTabId : function() {
		if (this.tagId)
			return this.tagId;
		if (frameElement.attributes.tabid)
			return frameElement.attributes.tabid.value;
		else
			throw new Error("标签页中未定义tabid属性");
	}
});